package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.common.response.JsonResult;
import cn.tedu._05weibo.common.response.StatusCode;
import cn.tedu._05weibo.mapper.WeiboMapper;
import cn.tedu._05weibo.pojo.dto.WeiboDTO;
import cn.tedu._05weibo.pojo.entity.Weibo;
import cn.tedu._05weibo.pojo.vo.UserVO;
import cn.tedu._05weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._05weibo.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "02.微博模块")
/**
 * 校验声明参数方式接收的数据时,一定要在控制器类上添加@Validated注解
 */
@Validated
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    //自动装配
    @Autowired
    private WeiboMapper weiboMapper;

    /**
     * 1.发布微博功能
     */
    @ApiOperation(value = "发布微博")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        /*
            1.校验用户登录状态
            2.未登录状态:return 2
            3.登录状态:插入数据[接口方法],return 1
         */
        UserVO userVO = (UserVO)session.getAttribute("user");
//        if (userVO==null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        //将数据存入微博表[实体类]
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());
        weiboMapper.insert(weibo);
        return JsonResult.ok();
    }

    /**
     * 2.首页微博列表功能
     */
    @ApiOperation(value = "微博首页")
    @GetMapping("selectIndex")
    public JsonResult selectIndex(){
        return JsonResult.ok(weiboMapper.selectIndex());
    }

    /**
     * 3.微博详情功能
     */
//    @ApiImplicitParam(name = "id",value = "微博编号",required = true,dataType = "Long")
//    @ApiImplicitParam(name = "username",value = "用户名",required = true,dataType = "String")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "微博编号",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "username",value = "用户名",required = true,dataType = "string")
    })
    @ApiOperation(value = "微博详情")
    @GetMapping("selectById")
    public JsonResult selectById(@Range(min = 200,max=600,message = "微博编号必须在200-600之间") @RequestParam Long id, String username){
        //手动抛出异常
        if (id<0){
            throw new IllegalArgumentException("ID值是无效");
        }
        //username参数仅仅是为了测试Knife4j的注解，没有其他作用
        return new JsonResult(StatusCode.OPERATION_SUCCESS,weiboMapper.selectById(id));
    }
}
